#include <stdio.h>
#include <MyString.h>
#include <time.h>
#include <stdlib.h>
#include "LinkStack.h"
#include "LinkQueue.h"

#define StopSize 3
#define PerPrice 5
#define Clean while(getchar() != '\n');

MyStr GetTimeStr(const long t)
{
    struct tm *m = gmtime(&t);
    INITSTR(str);
    ResizeSting(&str,100);
    sprintf(str.string,"%d年%d月%d日 %d时%d分%d秒",m->tm_year + 1900,m->tm_mon + 1,m->tm_mday,(m->tm_hour + 8) % 24,m->tm_min,m->tm_sec);
    return str;
}


typedef struct 
{
    MyStr name;
    long arriveTime;
    long leaveTime;
}Car;

Car* CreateCar(const char *name,long arriveTime)
{
    Car* c = (Car*)malloc(sizeof(Car));
    if(c == NULL)
        return NULL;
    c->name = GetString(name);
    c->arriveTime = arriveTime;
}

void FreeCar(void *ptr)
{
    Car *c = (Car*)ptr;
    ClearString(&c->name);
    free(c);
}

void PrintCar(void *ptr)
{
    Car* c = (Car *)ptr;
    MyStr t = GetTimeStr(c->arriveTime);
    printf("车牌:|%s|,到达时间:|%s|\n",c->name.string,t.string);
    ClearString(&t);
}
void ComeCar(LStack *park,LQueue *pavement)
{
    printf("请输入车牌:");
    char str[100] = {0};
    scanf("%s\n",str);
    Clean;

    long t = time(NULL);
    Car *newCar = CreateCar(NULL,0);
    if(newCar == NULL)
        return;
    if(GetStackLen(park) < StopSize)
    {
        LSPush(park,newCar);
        printf("停车场没停满,车辆|%s|在%s停在了第%d个位置\n",newCar->name.string,GetTimeStr(newCar->arriveTime).string,GetStackLen(park));
    }
    else
    {
        printf("停车场已满,车辆|%s|在%s停在便道第%d个位置\n",newCar->name.string,GetTimeStr(newCar->arriveTime).string,GetQueueLen(pavement));
        LQPush(pavement,newCar);
    }
}

void LeaveCar(LStack *park,LQueue *pavement)
{
    printf("请输入要离开的车牌号\n");
    char carName[100] = {0};
    scanf("%s",carName);
    Clean;

    LStack *temp = InitLinkStack();
    double leaveTime = 0.0;
    Car *c = (Car*)*GetSTop(park);
    while(IsStackEmpty(park) == false)
    {
        if(IsEqual(&c->name,carName) == true)
        {
            c->leaveTime = time(NULL);
            leaveTime = c->leaveTime;

            printf("车辆|%s|在|%s|离开停车场,费用为%ld",c->name.string,GetTimeStr(c->leaveTime).string,(c->leaveTime-c->arriveTime)*PerPrice/3600);
            LPop(park);
            FreeCar(c);
            break;
        }
        else
        {
            LSPush(temp,c);
            LPop(park);
        }
    }
    while(IsStackEmpty(temp) == false)
    {
        LSPush(park,*GetSTop(temp));
        LPop(temp);
    }
    ClearLStack(temp,NULL);

    Car *frontCar = (Car*)GetLQFront(pavement);
    LQPop(pavement);
    if(GetStackLen(park) < 3 && GetQueueLen(pavement) > 0)
    {
        printf("车辆|%s|在|%s|从便道上开到停车场",frontCar->name.string,GetTimeStr(c->leaveTime).string);
        LSPush(park,frontCar);
    }
}


int main()
{
    LStack *park = InitLinkStack();
    LQueue *pavement = InitLinkQueue();

    while(1)
    {
        printf("=================停车场====================\n");
        printf("1.车辆进入停车场\n");
        printf("2.车辆离开停车场\n");
        printf("3.显示停车场信息\n");
        printf("4.显示便道信息\n");
        printf("5.显示停车场出车流水信息\n");
        printf("6.退出\n");
        printf("===========================================\n");

        int choice = 0;
        scanf("%d",&choice);
        Clean;

        switch (choice)
        {
        case 1:
            system("clear");
            ComeCar(park,pavement);
            break;
        case 2:
            system("clear");
            LeaveCar(park,pavement);
            break;
        case 3:
            system("clear");
            TravelLStack(park,PrintCar);
            break;
        case 4:
            system("clear");
            TravelQueue(pavement,PrintCar);
            break;
            default:
            break;
        }
    }
    return 0;
}
